﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hDualPane.Model;
using System.Collections.ObjectModel;
using System.IO;

namespace hDualPane.Controller
{
    class FileManager
    {
        ObservableCollection<FileSummary> _leftList = new ObservableCollection<FileSummary>();
        ObservableCollection<FileSummary> _rightList = new ObservableCollection<FileSummary>();

        private string _leftPath = "";

        public string LeftPath
        {
            get { return _leftPath; }
        }
        private string _rightPath = "";

        public string RightPath
        {
            get { return _rightPath; }            
        }

        public FileManager(string fullPath)
        {
            _leftList = getFilesList(fullPath);
            _rightList = _leftList;
            _leftPath = fullPath;
            _rightPath = _leftPath;
        }

        public ObservableCollection<FileSummary> getFilesList(string fullPath)
        {
            //The result list
            ObservableCollection<FileSummary> list = new ObservableCollection<FileSummary>();
            
            //Directories list
            string[] dirsList = Directory.GetDirectories(fullPath);
            //File list
            string[] filesList = Directory.GetFiles(fullPath);

            list.Add(new FileSummary
            {
                FileName = "[BACK]",
                FileExt = "",
                FileSize = -1
            });

            //List directories
            for (int i = 0; i < dirsList.Length; i++)
            {
                FileSummary dir = new FileSummary();
                dir.FileName = Path.GetFileNameWithoutExtension(dirsList[i]);
                dir.FileExt = "<DIR>";
                //Negative size because this is a directory
                dir.FileSize = -1;
                list.Add(dir);

            }

            //List files
            for (int i = 0; i < filesList.Length; i++)
            {
                FileSummary file = new FileSummary();
                FileInfo info = new FileInfo(filesList[i]);

                file.FileName = Path.GetFileNameWithoutExtension(filesList[i]);
                file.FileExt = Path.GetExtension(filesList[i]).ToLower();
                file.FileSize = info.Length;

                list.Add(file);
            }

            return list;
        }

        public ObservableCollection<FileSummary> LeftList
        {
            get { return _leftList; }
        }

        public ObservableCollection<FileSummary> RightList
        {
            get { return _rightList; }
        }
    }
}
